/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.edit;

import com.daikit.daikit4gxt.client.ui.edit.EditButtonSelectEvent.EditButtonSelectHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;


/**
 * Fires after an Edit button is selected
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <M>
 *           the type of the edited object
 */
@SuppressWarnings("rawtypes")
public class EditButtonSelectEvent<M> extends GwtEvent<EditButtonSelectHandler>
{
	/**
	 * Handler type.
	 */
	private static Type<EditButtonSelectHandler> TYPE;

	private M editedObject;

	/**
	 * Gets the type associated with this event.
	 * 
	 * @return returns the handler type
	 */
	public static Type<EditButtonSelectHandler> getType()
	{
		if (TYPE == null)
		{
			TYPE = new Type<EditButtonSelectHandler>();
		}
		return TYPE;
	}

	/**
	 * Constructor
	 * 
	 * @param editedObject
	 *           the edited object
	 */
	public EditButtonSelectEvent(final M editedObject)
	{
		this.editedObject = editedObject;
	}

	@Override
	public Type<EditButtonSelectHandler> getAssociatedType()
	{
		return TYPE;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void dispatch(final EditButtonSelectHandler handler)
	{
		handler.onSelect(this);
	}

	/**
	 * @return the editedObject
	 */
	public M getEditedObject()
	{
		return editedObject;
	}

	/**
	 * @param editedObject
	 *           the editedObject to set
	 */
	public void setEditedObject(final M editedObject)
	{
		this.editedObject = editedObject;
	}

	/**
	 * Handler for {@link EditButtonSelectEvent} events.
	 * 
	 * @param <M>
	 *           the type of the object to be edited
	 */
	public interface EditButtonSelectHandler<M> extends EventHandler
	{
		/**
		 * Called after a Edit button is selected.
		 * 
		 * @param event
		 *           the {@link SelectEvent} that was fired
		 */
		void onSelect(EditButtonSelectEvent<M> event);
	}

	/**
	 * A widget that implements this interface is a public source of {@link SelectEvent} events.
	 * 
	 * @param <M>
	 *           the type of the object to be edited
	 */
	public interface HasEditButtonSelectHandlers<M>
	{

		/**
		 * Edits a {@link SelectHandler} handler for {@link EditButtonSelectEvent} events.
		 * 
		 * @param handler
		 *           the handler
		 * @return the registration for the event
		 */
		HandlerRegistration addEditSelectHandler(EditButtonSelectHandler<M> handler);
	}
}
